package com.soonfor.systemcodes;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.manager.NetManager;
import com.google.gson.Gson;
import com.lidroid.xutils.http.RequestParams;
import com.soonfor.evaluate.tools.Tokens;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * author : DC-DingYG
 * e-mail : dingyg012655@126.com
 * time : 2020/11/09
 * desc : * 系统参数
 */
public class SystemCodePresenter {
    private CallBack callBack;

    public SystemCodePresenter(CallBack callBack) {
        this.callBack = callBack;
    }

    /**
     * 一次可获取一个
     *
     * @param code 要请求的参数
     * @param requestCode   请求口令 10000
     */
    public void getOneSystemParams(final String code, final int requestCode, int outTime) {
        // 为子类准备一个联网类，具体联网成功和失败都由子类来做处理
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RequestParams params = new RequestParams();
                params.addBodyParameter("sf_action", "getsyscode");
                params.addBodyParameter("usercode", MyApplication.getUserCode());
                params.addBodyParameter("fcodeid", code);
                new NetManager() {
                    @Override
                    public void onNetWorkSuccess(int code, String result) {
                        try {
                            String data = new JSONObject(result).getString("data");
                            callBack.onSingle(code, true, data, null);
                        } catch(JSONException e){
                            callBack.onSingle(code, false, null, null);
                        }
                    }

                    @Override
                    public void onNetWorkErorr(int code, String result) {
                        callBack.onSingle(code, false, "1", null);
                    }
                }.connectWork(params, requestCode);
            }
        }, outTime);
    };

    /**
     * 一次可请求多个系统参数值
     *暂无接口
     * @param mContext
     * @param codes 要请求的参数
     */
    public void getMultSystemParams(final Context mContext, final String[] codes) {
        JSONArray fcodeids = new JSONArray();
        try {

            for (String code : codes) {
                JSONObject joCode = new JSONObject();
                joCode.put("fCodeId", code);
                fcodeids.put(joCode);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        // 为子类准备一个联网类，具体联网成功和失败都由子类来做处理
        RequestParams params = new RequestParams();
        params.addBodyParameter("sf_action", "getsyscode");
        params.addBodyParameter("usercode", MyApplication.getUserCode());
        params.addBodyParameter("fcodeid", fcodeids.toString());
        new NetManager() {
            @Override
            public void onNetWorkSuccess(int code, String result) {
                if (code == 7777){
                    try {
                        JSONObject jo = new JSONObject(result);
                        String data = new JSONObject(result).getString("data");
                        if (!TextUtils.isEmpty(data)) {
                            try {
                                JSONArray ja = new JSONArray(data);
                                Gson gson = new Gson();
                                if (ja != null && ja.length() > 0) {
                                    for (int i = 0; i < ja.length(); i++) {
                                        SystemCodeBean scBean = gson.fromJson(ja.getJSONObject(i).toString(), SystemCodeBean.class);
                                        SystemCodes.getInstance().setValues(mContext, scBean);
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            callBack.onMult(true, null);
                        }
                    } catch (JSONException e) {
                        callBack.onMult(false, e.getMessage());
                    }
                }
            }

            @Override
            public void onNetWorkErorr(int code, String result) {
                if (code == 102){
                    callBack.onMult(false, "");
                }
            }
        }.connectWork(params, 7777);
    }


    public interface CallBack {
        //请求某一个参数
        void onSingle(int type, boolean isSuccess, String data, String fullmsg);
        //请求多个参数
        @Nullable
        void onMult(boolean isSuccess, String fullmsg);
    }
}
